package View.GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Controller.Game.GamePanelController;
import Model.Visible.Planet;
import Utils.ActionButton;
import Utils.ButtonImage;
import Utils.ConstantsImplement;
import Utils.Utils;
import View.Game.GamePanel;

	/**
	 * 
	 * This class is the panel to perform actions in the planets.<dd>
	 *</dd>
	 *  @autor Magal
	 *  
	 */

public class ActionPlanetPanel extends JPanel implements ConstantsImplement {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Planet planet_;
	private PlanetGUI planetGUI_;
	private BufferedImage background_;
	private ButtonImage createIndustry_;
	private ButtonImage createFleetMaker_;
	private ButtonImage createFleet_;
	private ButtonImage createDefenses_;
	private ButtonImage updateIndustry_;
	private ButtonImage updateFleetMaker_;
	private ButtonImage increaseFleet_;
	private ButtonImage decreaseFleet_;
	private ButtonImage updateDefenses_;
	private ButtonImage exit_;
	private JLabel industryLabel_;
	private JLabel resourceLabel_;
	private JLabel fleetMakerLabel_;
	private JLabel fleetLabel_;
	private JLabel fleetNumberOf_;
	private JLabel defensesLabel_;
	private JLabel populationLabel_;
	
	
	/**
	 * ActionPlanetPanel Constructor
	 */
	public ActionPlanetPanel(Planet planet, PlanetGUI planetGUI, GamePanelController gamePanelController) {
		// Principal parameters
		setLayout(null);
		setLocation(150, 50);
		setSize(500, 500);
		URL url = GamePanel.class.getResource("/Image/Menu1Action.png");
		background_ = Utils.toBufferedImage((new ImageIcon(url)).getImage());
		planet_ = planet;
		planetGUI_ = planetGUI;
		
		//Buttons definitions
		createIndustry_ = new ButtonImage(this, gamePanelController.getMedia_().createIndustryButton1_, gamePanelController.getMedia_().createIndustryButton2_,86, 226, 234, 31);
		createFleetMaker_ = new ButtonImage(this, gamePanelController.getMedia_().createFleetMakerButton1_, gamePanelController.getMedia_().createFleetMakerButton2_,86, 288, 234, 31);
		createFleet_ = new ButtonImage(this, gamePanelController.getMedia_().createFleet1_, gamePanelController.getMedia_().createFlee2_,86, 352, 234, 31);
		createFleet_.setEnable(false);
		createDefenses_ = new ButtonImage(this, gamePanelController.getMedia_().createDefensesButton1_, gamePanelController.getMedia_().createDefensesButton2_,86, 419, 234, 31);
		exit_ = new ButtonImage(this,gamePanelController.getMedia_().exit_ , gamePanelController.getMedia_().exit_ , 473, 10, 17, 17);
		updateIndustry_ = new ButtonImage(this, gamePanelController.getMedia_().updateIndustryButton1_, gamePanelController.getMedia_().updateIndustryButton2_ ,86, 226, 234, 31);
		updateIndustry_.setEnable(false);
		updateFleetMaker_ = new ButtonImage(this, gamePanelController.getMedia_().updateFleetMakerButton1_, gamePanelController.getMedia_().updateFleetMakerButton2_,86, 288, 234, 31);
		updateFleetMaker_.setEnable(false);
		updateDefenses_ = new ButtonImage(this, gamePanelController.getMedia_().updateDefensesButton1_, gamePanelController.getMedia_().updateDefensesButton2_,86, 419, 234, 31);
		updateDefenses_.setEnable(false);
		increaseFleet_ = new ButtonImage(this, gamePanelController.getMedia_().increaseFleet_, gamePanelController.getMedia_().increaseFleet_,420, 360);
		increaseFleet_.setEnable(false);
		decreaseFleet_ = new ButtonImage(this, gamePanelController.getMedia_().decreaseFleet_, gamePanelController.getMedia_().decreaseFleet_,340, 360);
		decreaseFleet_.setEnable(false);
		//Instancing Labels
		if (planet_.getIndustry_() == null){
			industryLabel_ = new JLabel("Price: " + INITIAL_INDUSTRY_PRICE);

		}else{
			industryLabel_ = new JLabel(planet_.getIndustry_().toString());

		}
		if(planet_.getFleetMaker_() == null){
			fleetMakerLabel_ = new JLabel("Price: " + INITIAL_FLEET_MAKER_PRICE);
		}else{
			fleetMakerLabel_ = new JLabel(planet_.getFleetMaker_().toString());
		}
		if(planet_.getFleet_() == null ){
			fleetLabel_ = new JLabel("Fleet ");
		}else{
			fleetLabel_ = new JLabel(planet_.getFleet_().toString());
		}
		if(planet_.getDefences_() == null){
			defensesLabel_ = new JLabel("Price: " + INITIAL_DEFENSES_PRICE);
		}else{
			defensesLabel_ = new JLabel(planet_.getDefences_().toString());
		}
		fleetNumberOf_ = new JLabel(String.valueOf(planet_.getShipsToCreate_()));
		
		resourceLabel_ = new JLabel(String.valueOf(planet_.getResources()));
		populationLabel_ = new JLabel(String.valueOf(planet_.getPopulation_()));
		
		//Setting locations
		industryLabel_.setLocation(179, 212);
		resourceLabel_.setLocation(181, 148);
		fleetMakerLabel_.setLocation(210, 274);
		fleetLabel_.setLocation(145, 338);
		defensesLabel_.setLocation(170 , 402);
		populationLabel_.setLocation(181, 102);
		fleetNumberOf_.setLocation(370, 360);
		
		// Set Sizes
		industryLabel_.setSize(200, 12);
		resourceLabel_.setSize(200, 12);
		fleetMakerLabel_.setSize(200, 12);
		fleetLabel_.setSize(200, 12);
		defensesLabel_.setSize(200, 12);
		populationLabel_.setSize(200, 12);
		fleetNumberOf_.setSize(50, 12);
		
		//Set Font
		industryLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		industryLabel_.setForeground(Color.WHITE);
		fleetMakerLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		fleetMakerLabel_.setForeground(Color.WHITE);
		fleetLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		fleetLabel_.setForeground(Color.WHITE);
		defensesLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		defensesLabel_.setForeground(Color.WHITE);
		resourceLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		resourceLabel_.setForeground(Color.WHITE);
		populationLabel_.setFont(new Font("sansserif", Font.BOLD, 12));
		populationLabel_.setForeground(Color.WHITE);
		fleetNumberOf_.setFont(new Font("sansserif", Font.BOLD, 12));
		fleetNumberOf_.setForeground(Color.WHITE);
		
		//Add
		add(populationLabel_);
		add(resourceLabel_);
		add(industryLabel_);
		add(fleetMakerLabel_);
		add(fleetLabel_);
		add(defensesLabel_);
		addActions();
		addMouseMotionListener(createFleetMaker_);
		addMouseListener(createFleetMaker_);
		addMouseMotionListener(createDefenses_);
		addMouseListener(createDefenses_);
		addMouseMotionListener(createIndustry_);
		addMouseListener(createIndustry_);
		addMouseMotionListener(exit_);
		addMouseListener(exit_);
	}
	
	public void paintComponent(Graphics g){
		updateLabel();
		super.paintComponents(g);
		g.setColor( new Color( 240, 230, 140, 0) );
		g.fillRect( 0, 0, getWidth(), getHeight() );
		g.drawImage(background_, 0, 0, getWidth(), getHeight(), null);
		exit_.draw(g);
		createDefenses_.draw(g);
		createFleet_.draw(g);
		createFleetMaker_.draw(g);
		createIndustry_.draw(g);
		updateIndustry_.draw(g);
		updateFleetMaker_.draw(g);
		updateDefenses_.draw(g);
		increaseFleet_.draw(g);
		decreaseFleet_.draw(g);
	}
	
	private void updateLabel() {
		// TODO Auto-generated method stub
		if (planet_.getIndustry_() == null){
			industryLabel_.setText("Price: " + INITIAL_INDUSTRY_PRICE);

		}else{
			industryLabel_.setText("Level: " + planet_.getIndustry_().getLevel() + " Price: " +  planet_.getIndustry_().getPrice());

		}
		if(planet_.getFleetMaker_() == null){
			fleetMakerLabel_.setText("Price: " + INITIAL_FLEET_MAKER_PRICE);
		}else{
			fleetMakerLabel_.setText("Level: " + planet_.getFleetMaker_().getLevel() + " Price: " + planet_.getFleetMaker_().getPrice());
		}
		if(planet_.getFleetMaker_() == null ){
			fleetLabel_.setText(" Price: " + planet_.getShipsToCreate_()*INITIAL_SHIP_PRICE);
		}else{
			fleetLabel_.setText(planet_.getFleet_().getNumShips() + " Price: " + planet_.getFleetMaker_().getShipPrice_()*planet_.getShipsToCreate_());
		}
		if(planet_.getDefences_() == null){
			defensesLabel_.setText("Price: " + INITIAL_DEFENSES_PRICE);
		}else{
			defensesLabel_.setText("Level: " + planet_.getDefences_().getLevel() + " Price: " + planet_.getDefences_().getPrice());
		}
		resourceLabel_.setText(String.valueOf(planet_.getResources()));
		populationLabel_.setText(String.valueOf(planet_.getPopulation_()));
		fleetNumberOf_.setText(String.valueOf(planet_.getShipsToCreate_()));
	}

	//Functions
	private void addActions(){
		createDefenses_.addActionButton(new ActionButton(){

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				
				if (planet_.createDefences()){
					ActionPlanetPanel.this.removeMouseListener(ActionPlanetPanel.this.createDefenses_);
					ActionPlanetPanel.this.removeMouseMotionListener(ActionPlanetPanel.this.createDefenses_);
					ActionPlanetPanel.this.createDefenses_.setEnable(false);
					ActionPlanetPanel.this.addMouseListener(ActionPlanetPanel.this.updateDefenses_);
					ActionPlanetPanel.this.updateDefenses_.setEnable(true);
				}
				
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();

			}
			
		});
		updateDefenses_.addActionButton(new ActionButton(){

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				
				planet_.updateDefenses();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
			}
			
		});
		createFleet_.addActionButton(new ActionButton() {

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				planet_.createFleet();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();

			}
		});
		createFleetMaker_.addActionButton(new ActionButton() {

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				if ( planet_.createFleetMaker()) {
					ActionPlanetPanel.this.removeMouseListener(ActionPlanetPanel.this.createFleetMaker_);
					ActionPlanetPanel.this.removeMouseMotionListener(ActionPlanetPanel.this.createFleetMaker_);
					ActionPlanetPanel.this.createFleetMaker_.setEnable(false);
					ActionPlanetPanel.this.addMouseListener(ActionPlanetPanel.this.updateFleetMaker_);
					ActionPlanetPanel.this.updateFleetMaker_.setEnable(true);
					ActionPlanetPanel.this.addMouseMotionListener(createFleet_);
					ActionPlanetPanel.this.addMouseListener(createFleet_);
					ActionPlanetPanel.this.createFleet_.setEnable(true);
					ActionPlanetPanel.this.increaseFleet_.setEnable(true);
					ActionPlanetPanel.this.addMouseListener(ActionPlanetPanel.this.increaseFleet_);
					ActionPlanetPanel.this.decreaseFleet_.setEnable(true);
					ActionPlanetPanel.this.addMouseListener(ActionPlanetPanel.this.decreaseFleet_);
					ActionPlanetPanel.this.add(fleetNumberOf_);
				}
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
			}
		});
		updateFleetMaker_.addActionButton(new ActionButton(){

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				planet_.updateFleetMaker();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
			}
			
		});
		createIndustry_.addActionButton(new ActionButton() {

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				if (planet_.createIndustry()){
				ActionPlanetPanel.this.removeMouseListener(ActionPlanetPanel.this.createIndustry_);
				ActionPlanetPanel.this.removeMouseMotionListener(ActionPlanetPanel.this.createIndustry_);
				ActionPlanetPanel.this.createIndustry_.setEnable(false);
				ActionPlanetPanel.this.addMouseListener(ActionPlanetPanel.this.updateIndustry_);
				ActionPlanetPanel.this.updateIndustry_.setEnable(true);
				}
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
			}
		});
		updateIndustry_.addActionButton(new ActionButton(){

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				
				planet_.updateIndustry();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
			}
			
		});
		exit_.addActionButton(new ActionButton() {

			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				//System.out.println("enter"+ this);
				ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().remove(ActionPlanetPanel.this);
				ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();
				ActionPlanetPanel.this.planetGUI_.setActionPlanetPanelUp(false);
			}
		});
		increaseFleet_.addActionButton(new ActionButton() {

			@Override
			public void enterAction() {
				// TODO Auto-generated method stub
				//System.out.println("enter"+ this);
			}

			@Override
			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				//System.out.println("pressed"+ this);
				ActionPlanetPanel.this.planet_.increaseShipsToCreate_();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();

			}
		});
		decreaseFleet_.addActionButton(new ActionButton() {

			@Override
			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void exitAction() {
				// TODO Auto-generated method stub
				
			}
			

			@Override
			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				//System.out.println("enter"+ this);
				ActionPlanetPanel.this.planet_.decreaseShipsToCreate_();
				//ActionPlanetPanel.this.planetGUI_.getGamePanelController_().getGamePanel_().repaint();

			}
		});

		
	}
}
